package com.xiaorun.portal.spider.pipeline;

import com.xiaorun.portal.dao.NewsAttachMapper;
import com.xiaorun.portal.dao.NewsMapper;
import com.xiaorun.portal.modal.Vo.News;
import com.xiaorun.portal.modal.Vo.NewsAttach;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import javax.annotation.Resource;
import java.util.List;

@Component
public class NewsPipeline implements Pipeline {

    @Resource
    private NewsMapper newsMapper;

    @Resource
    private NewsAttachMapper newsAttachMapper;

    @Override
    public void process(ResultItems resultItems, Task task) {
        News news = resultItems.get("news");
        if (null != news) {
            newsMapper.save(news);
            List<NewsAttach> newsAttachList = (List<NewsAttach>) resultItems.get("newsAttachList");
            if (newsAttachList != null && newsAttachList.size() > 0) {
                for (NewsAttach newsAttach : newsAttachList) {
                    newsAttach.setNewsId(news.getId());
                }
                newsAttachMapper.batchSave(newsAttachList);
            }
        }
    }

}
